package com.beg.cant.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.beg.cant.comment.utils.UniqueIdGenerator;
import com.beg.cant.entity.*;
import com.beg.cant.mapper.GiftMapper;
import com.beg.cant.mapper.UserMapper;
import com.beg.cant.mapper.WithdrawMapper;
import com.beg.cant.service.OrderService;
import com.beg.cant.service.PayService;
import com.beg.cant.service.UserService;
import com.beg.cant.service.WithdrawService;
import com.beg.cant.vo.OrderPaymentVO;
import com.beg.cant.vo.WXPayOrderReqVO;
import com.beg.cant.vo.WithdrawVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import com.beg.cant.comment.utils.WeChatPayUtil;
@Slf4j
@Service
public class PayServiceImpl implements PayService {
    @Autowired
    private WeChatPayUtil weChatPayUtil;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private GiftMapper giftMapper;
    @Autowired
    private WithdrawService withdrawService;
    @Autowired
    private OrderService orderService;
    @Override
    public OrderPaymentVO payOrder(WXPayOrderReqVO req) throws Exception {
        User user = userMapper.selectById(req.getUserId());
        Gift gift = giftMapper.selectById(req.getGoodId());
        String str = String.valueOf(UUID.randomUUID()).substring(0,32);
        log.info("礼物信息{}",gift);
        //调用微信支付接口，生成预支付交易单
        JSONObject jsonObject = weChatPayUtil.pay(
                str, //商户订单号
                gift.getPrice(), //支付金额，单位 元
                gift.getName(), //商品描述
                user.getOpenid() //微信用户的openid
        );
        OrderPaymentVO javaObject = jsonObject.toJavaObject(OrderPaymentVO.class);
        OrderPaymentVO vo = jsonObject.toJavaObject(OrderPaymentVO.class);
        vo.setPackageStr(jsonObject.getString("package"));
        orderService.createOrder(Order.builder()
                        .userId(user.getId())
                        .outTradeNo(str)
                        .status(0)
                        .postsId(req.getPostId())
                        .amount(gift.getPrice())
                        .createdAt(LocalDateTime.now())
                .build());
        return vo;
    }

    /**
     * 功能描述:
     * 〈微信支付回调〉
     *
     * @param code
     */
    @Override
    public void paySuccess(String code) {
        orderService.paySuccess(code);
    }

    /**
     * 提现
     *
     * @param code
     */
    @Override
    public TransferBill withdraw(String code) throws Exception {
        WithdrawVO withdraw = withdrawService.selectWithdraw(code);
        log.info("这是withdraw的数据{}",withdraw);
        TransferBill transferBill = weChatPayUtil.transferMethod(TransferDetail.builder()
                .openid(withdraw.getOpenid())
                .transferRemark("用户提现")
                .transferAmount(withdraw.getAmount())
                .build());
        log.info("这是transferBill的数据{}",transferBill);
        // 若是状态为用户待领取状态则修改记录订单号WAIT_USER_CONFIRM
        if (transferBill.getState()== TransferBill.TransferState.WAIT_USER_CONFIRM) {
            log.info("更改数据了");
            withdrawService.upWithdraw(withdraw.getId(),transferBill.getOutBillNo());
        }
        return transferBill;
    }
}
